<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		$this->load->view('header');
		$this->load->view('home');
		$this->load->view('footer');
	}

	public function do_exam()
	{ 
		$this->load->database();
		$this->load->view('header');
        $this->load->library('session');

        $sql          = 'select * from questions';
        $query        = $this->db->query($sql);
        $data['rows'] = $query->result();
        $data['user_name'] = $this->session->userdata('user_id') ? $this->session->userdata('user_id') : '匿名';


		$this->load->view('do_exam', $data); 
		$this->load->view('footer');
	}

	public function all_questions()
	{ 
		$this->load->database();
		$this->load->view('header');

        $sql          = 'select * from questions';
        $query        = $this->db->query($sql);
        $data['rows'] = $query->result();

		$this->load->view('do_exam', $data); 
		$this->load->view('footer');
	}

	public function add_questions()
	{ 
        if ( $_POST ) {
            $question = $_POST['question'];
            $answer   = $_POST['answer'];

            if ( $question == '' or $answer == '' ) { 
                echo '试题和答案不能为空！';
            } else {
                $this->load->database();
                $sql         = "insert into questions (question) values ('$question')";
                $query       = $this->db->query($sql);
                $question_id = mysql_insert_id();

                $sql   = "insert into answers (question_id, answer) values ($question_id, '$answer')";
                $query = $this->db->query($sql);
                echo "新试题添加成功！"; 
            } 
        } else {
            echo 'no post'; 
        }

		$this->load->view('header'); 
		$this->load->view('add_questions'); 
		$this->load->view('footer');
	}

	public function edit_question($question_id=0) 
	{ 
        if ( $question_id == 0 ) {
            echo "没有指定试题ID!";
        } 
        
        if ( $_POST ) {
            $question = $_POST['question'];
            $answer   = $_POST['answer'];

            if ( $question == '' or $answer == '' ) { 
                echo '试题和答案不能为空！';
            } else {
                $this->load->database();
                $sql   = "update questions set question = '$question' where id = $question_id ";
                $query = $this->db->query($sql);

                $sql   = "update answers set answer = '$answer' where question_id = $question_id ";
                $query = $this->db->query($sql);
                echo "试题修改成功！"; 
            } 
        }
       
        // show question
        $this->load->database();
        $sql              = "select * from questions where id = $question_id ";
        $query            = $this->db->query($sql);
        $rows             = $query->result_array();
        $data['question'] = $rows[0]['question'];

        $sql = "select * from answers where question_id = $question_id "; 
        $query = $this->db->query($sql);
        if ( $query->num_rows == 0 ) {
            $data['answers'] = ''; 
        } else {
            $rows = $query->result_array();
            $data['answers'] = $rows[0]['answer'];
        }


        $data['question_id'] = $question_id;


		$this->load->view('header'); 
		$this->load->view('edit_question', $data); 
		$this->load->view('footer');
	}

	public function exams()
	{
        $this->load->view('header');
        $this->load->view('exams');
        $this->load->view('footer');
	}

	public function register()
	{
        if ( $_POST ) {
            $email = $_POST['email'];
            $name  = $_POST['name'];
            $pwd   = $_POST['pwd'];

            if ( $email == '' or $name == '' or $pwd == '' ) { 
                echo '用户名、邮箱、密码不能为空！';
            } else {
                $this->load->database();
                $pwd   = md5($name.$pwd);
                $sql   = "insert into users (email, name, pwd) values ('$email', '$name', '$pwd')";
                $query = $this->db->query($sql);

                echo "注册成功！"; 
            } 
        } else { 
            $this->load->view('header');
            $this->load->view('register');
            $this->load->view('footer');
        }
	}

	public function user($name='')
	{
        if ( $name == '' ) {
            echo "没有用户名";
            exit();
        }

        $this->load->database();
        $sql   = "select * from users where name = '$name' ";
        $query = $this->db->query($sql);
        if ( $query->num_rows == 0 ) {
            echo "user no exist!"; 
        } else {
            $rows = $query->result_array(); 
            $data['user_info'] = $rows[0]; 
        } 

		$this->load->view('header');
		$this->load->view('user', $data);
		$this->load->view('footer');
	}

	public function login()
	{
        if ( $_POST ) {
            $name  = $_POST['name'];
            $pwd   = $_POST['pwd'];

            if ( $name == '' or $pwd == '' ) { 
                echo '用户名、密码不能为空！';
            } else {
                $this->load->database();
                $pwd   = md5($name.$pwd);
                $sql   = "select * from users where name = '$name' and pwd = '$pwd' ";
                $query = $this->db->query($sql);
                if ( $query->num_rows == 0 ) {
                    echo "用户名或密码出错！";
                }else {
                    $rows = $query->result_array();
                    $this->load->library('session');
                    $this->session->set_userdata('user_id', 'abc1');
                    //echo $this->session->userdata('user_id');
                    header("Location:user/$name"); 
                }

            } 
        }

		$this->load->view('header');
		$this->load->view('login');
		$this->load->view('footer');
	}

	public function logout()
	{
		$this->load->view('header');
		$this->load->view('logout');
		$this->load->view('footer');
	}


}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
